package com.ideal.manage.esms.service.project;

import com.ideal.manage.esms.bean.project.ControlOrder;
import com.ideal.manage.esms.bean.project.ParameterInfo;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.config.shiro.MyShiroRealm;
import com.ideal.manage.esms.repository.framework.MySpecification;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.repository.project.ControlOrderRepository;
import com.ideal.manage.esms.util.HttpRequests;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ControlOrderService {
    @Autowired
    ControlOrderRepository controlOrderRepository;

    @Cacheable(value = "projectPage")
    public Page<ControlOrder> findAllControlOrder(int pageNum, HttpServletRequest request) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        SpecificationOperator operator = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(operator);
        MySpecification<ControlOrder> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, 15, sort);
        Page<ControlOrder> page = controlOrderRepository.findAll(mySpecifications, pageable);
        return page;
    }

    public void saveControlOrderInfo(String id, String code, String name, Integer objName, Integer isControl) {
        MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        Date date = new Date();
        ControlOrder controlOrder = new ControlOrder();
        String uuid = "";
        if (id != null) {
            controlOrder = findById(id);
            controlOrder.setCreateUser(new User(shiroUser.getId()));
            controlOrder.setUpdateTime(new Date());
        } else {
            uuid = UUID.randomUUID().toString().replaceAll("-", "");
            controlOrder.setId(uuid);
            controlOrder.setCreateTime(date);
            controlOrder.setCreateUser(new User(shiroUser.getId()));
            controlOrder.setIsValid(1);
        }

            controlOrder.setCode(code);
            controlOrder.setName(name);
            controlOrder.setObjName(objName);
            if(isControl==null){
                isControl=0;
            }
            controlOrder.setIsControl(isControl);

            controlOrderRepository.save(controlOrder);

    }
    //参数测点编辑框回显数据
    public ControlOrder findById(String id) {
        ControlOrder code = controlOrderRepository.findOne(id);
        return code;
    }
    //参数测点删除
    public void delControlOrderInfos(String[] ids) {
        List<ControlOrder> controlOrdersInfo = controlOrderRepository.findAll(Arrays.asList(ids));

        controlOrderRepository.save(controlOrdersInfo);
    }
    //导出
    public List<ControlOrder> findAllEquipmentInfos() {
       return controlOrderRepository.findAll();
    }
    //导入数据
    public String  saveImport(List<String[]> list,String flag) {
        flag = "0";
        List<ControlOrder> allCode = controlOrderRepository.findCode();
        for (String [] info:list) {
            if(allCode.contains(info[0])){
                return flag = "1";
            }
            ControlOrder oinfo = new ControlOrder();
            oinfo.setCode(info[0]);
            oinfo.setName(info[1]);
            oinfo.setObjName(Integer.valueOf(info[2]));
            oinfo.setIsControl(Integer.valueOf(info[3]));


            MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            oinfo.setId(uuid);
            oinfo.setCreateTime(new Date());
            oinfo.setCreateUser(new User(shiroUser.getId()));
            oinfo.setIsValid(1);
            controlOrderRepository.save(oinfo);
        }
        return flag;
    }
    //查找code
    public List<ControlOrder> findCode() {
      List <ControlOrder> controlOrder =  controlOrderRepository.findCode();
       return controlOrder;
    }

}
